package com.example.springboot.controller.easypoicontroller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.example.springboot.pojo.easypoi.handler.MemberExcelDataHandler;
import com.example.springboot.pojo.easypoi.pojo.Member;
import com.example.springboot.pojo.easypoi.pojo.Order;
import com.example.springboot.pojo.easypoi.pojo.Product;
import com.example.springboot.utils.LocalJsonUtil;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/10/26 9:55
 */
@RestController
@RequestMapping("/easy")
public class EasyPoiController {
    /**
     * 下载会员列表并返回
     *
     * @param map
     * @param request
     * @param response
     * @return
     * @throws ParseException
     */
    @GetMapping("/poi")
    public void exportMemberList(ModelMap map,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws ParseException {
        List<Member> memberList = LocalJsonUtil.getListFromJson("json/member.json", Member.class);
        ExportParams exportParams = new ExportParams("会员列表", "会员列表", ExcelType.XSSF);
        // 设置处理器
        MemberExcelDataHandler handler = new MemberExcelDataHandler();
        // 设置需要特殊处理的类
        handler.setNeedHandlerFields(new String[]{"昵称"});
        exportParams.setDataHandler(handler);
        map.put(NormalExcelConstants.DATA_LIST, memberList);
        map.put(NormalExcelConstants.CLASS, Member.class);
        map.put(NormalExcelConstants.PARAMS, exportParams);
        map.put(NormalExcelConstants.FILE_NAME, "memberList");



        // 直接下载回去，返回给浏览器
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    /**
     * 导入
     *
     * @param file
     * @return
     */
    @RequestMapping("/importMemberList")
    public List<Member> importMemberList(@RequestParam("file") MultipartFile file) {
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        importParams.setTitleRows(1);
        try {
            List<Member> list = ExcelImportUtil.importExcel(file.getInputStream(),
                    Member.class, importParams);
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 复杂导出
     *
     * @param map
     * @param request
     * @param response
     */
    @GetMapping("/exportOrderList")
    public void exportOrderList(ModelMap map,
                                HttpServletRequest request,
                                HttpServletResponse response) {
        List<Order> orderList = getOrderList();
        ExportParams params = new ExportParams("订单列表", "订单列表", ExcelType.XSSF);
        //导出时排除一些字段
        params.setExclusions(new String[]{"ID", "出生日期", "性别"});
        map.put(NormalExcelConstants.DATA_LIST, orderList);
        map.put(NormalExcelConstants.CLASS, Order.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "orderList");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    private List<Order> getOrderList() {
        List<Order> orderList = LocalJsonUtil.getListFromJson("json/orders.json", Order.class);
        List<Product> productList = LocalJsonUtil.getListFromJson("json/products.json", Product.class);
        List<Member> memberList = LocalJsonUtil.getListFromJson("json/member.json", Member.class);
        for (int i = 0; i < orderList.size(); i++) {
            Order order = orderList.get(i);
            order.setMember(memberList.get(i));
            order.setProductList(productList);
        }
        return orderList;
    }
}
